#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define vec vector<int>
#define y1 y_____hahahaha_____1

using namespace std;

typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;

int read(){int x = 0,sgn = 1;char ch = getchar();for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;for(;isdigit(ch);ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); return x * sgn;}

const int N = 2e5+10;

int a[N],b[N],ta[N],n;
vector <pii> al,bl;
ll ans;

namespace BIT{
	int tr[N];
	void add(int x,int y){for(;x <= n;x += x & -x) tr[x] += y;}
	int query(int x,int y = 0){for(;x;x -= x & -x) y += tr[x];return y;}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in;
	for(int i = 1;i <= n;i++) a[i] = in + i,al.pb(pii(a[i],i));
	for(int i = 1;i <= n;i++) b[i] = in + i,bl.pb(pii(b[i],i));
	sort(al.begin(),al.end()); sort(bl.begin(),bl.end());
	for(int i = 0;i < n;i++) if(al[i].fi ^ bl[i].fi) return puts("-1"),0;
	for(int i = 0;i < n;i++) ta[al[i].se] = bl[i].se;
	for(int i = 1;i <= n;i++){
		BIT::add(ta[i],1); ans += BIT::query(n) - BIT::query(ta[i]);
	}
	printf("%lld\n",ans);
	return 0;
}
